Imports ViewStateEliminator_VB.ViewStateEliminator
Imports ViewStateEliminator_VB.ViewStateEliminator.Data
Imports ViewStateEliminator_VB.ViewStateEliminator.Configuration



Namespace ViewStateEliminator.Data
    Public Class ViewStateSessionStorage
        Implements ViewStateEliminator.Data.IViewstateStorage

        Private ReadOnly Property StorageList() As System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of Object, String))
            Get
                If IsNothing(System.Web.HttpContext.Current.Session("ViewStateSessionStorage")) Then
                    System.Web.HttpContext.Current.Session("ViewStateSessionStorage") = New System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of Object, String))()
                End If

                Return CType(System.Web.HttpContext.Current.Session("ViewStateSessionStorage"), System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of Object, String)))
            End Get
        End Property
        Public Sub Add(ByVal viewstateItem As System.Collections.Generic.KeyValuePair(Of Object, String)) Implements IViewstateStorage.Add
            StorageList.Add(viewstateItem)
        End Sub

        Public Function [Get](ByVal key As Object) As System.Collections.Generic.KeyValuePair(Of Object, String) Implements IViewstateStorage.Get
            Dim retItem As System.Collections.Generic.KeyValuePair(Of Object, String) = New System.Collections.Generic.KeyValuePair(Of Object, String)()

            For Each item As System.Collections.Generic.KeyValuePair(Of Object, String) In StorageList

                If (item.Key.ToString().ToLower().Equals(key.ToString().ToLower())) Then
                    retItem = item
                End If
            Next
            Remove(retItem.Key)
            Return retItem
        End Function

        Public Sub Remove(ByVal key As Object) Implements IViewstateStorage.Remove
            Dim itemToRemove As System.Collections.Generic.KeyValuePair(Of Object, String) = New System.Collections.Generic.KeyValuePair(Of Object, String)()
            For Each item As System.Collections.Generic.KeyValuePair(Of Object, String) In StorageList

                If (item.Key.ToString().ToLower().Equals(key.ToString().ToLower())) Then
                    itemToRemove = item
                End If
            Next
            StorageList.Remove(itemToRemove)
        End Sub
    End Class
End Namespace
